package com.help.help.service.impl;

import com.help.help.entity.UserEntity;
import com.help.help.service.UserService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.help.help.dao.FileSDao;
import com.help.help.entity.FileSEntity;
import com.help.help.service.FileSService;


@Service("fileSService")
public class FileSServiceImpl extends ServiceImpl<FileSDao, FileSEntity> implements FileSService {
    @Autowired
    private UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
          String key= (String) params.get("key");
        QueryWrapper<FileSEntity> wrapper = new QueryWrapper<>();
        QueryWrapper<UserEntity> userwrapper=new QueryWrapper<>();
        if(!StringUtil.isNullOrEmpty(key)){
             userwrapper.eq("name",key);
            UserEntity one = userService.getOne(userwrapper);
            if (one != null) {
                wrapper.eq("user_id", one.getId());
            } else {
                wrapper.eq("uuid", key);
            }

        }
        IPage<FileSEntity> page = this.page(
                new Query<FileSEntity>().getPage(params),
               wrapper
        );
        List<FileSEntity> list = page.getRecords();
        for (FileSEntity fileSEntity : list) {
             if(fileSEntity.getUserId()!=null) {
                 fileSEntity.setUser(userService.getById(fileSEntity.getUserId()));
             }
        }


        return new PageUtils(page);
    }

}